import 'dart:async';

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:shopping_group/baseblocs/bloc_provider.dart';
import 'package:shopping_group/http/apis/apis.dart';
import 'package:shopping_group/http/http_request_all.dart';
import 'package:shopping_group/utils/network_utils.dart';
import 'package:shopping_group/utils/route_util.dart';

class UserModifyPasswordBloc implements BlocBase {
  //  Stream来初始化数量的操作
  StreamController<Object> _saveActionController = StreamController<Object>();
  //初始化动作
  StreamSink get saveDate => _saveActionController.sink;

  UserModifyPasswordBloc() {
    _saveActionController.stream.listen(saveData);
  }

  void saveData(data) {
    if (data is Map<String, dynamic>) {
      _saveTask(data);
    }
  }

  void _saveTask(data) async {
    FocusScope.of(RouteUtil.navigatorKey.currentContext)
        .requestFocus(FocusNode());
    NetworkUtils.isNetworkConnected(connectedCallback: (connected) {
      if (!connected) {
        BotToast.showText(text: '网络无法连接');
      } else {
        HttpUtilAll.post(Apis.FIND_PASSWORD_RESET_URL, data: data, headers: {},
            success: (result) {
          BotToast.closeAllLoading();
          BotToast.showText(text: '修改密码成功');
          RouteUtil.closeView();
        }, error: (errorMsg) {
          BotToast.closeAllLoading();
          // 请求失败返回的错误信息
          BotToast.showText(text: errorMsg);
        });
      }
    });
  }

  @override
  void dispose() {
    _saveActionController?.close();
  }
}
